from selenium import webdriver
import time
import random
from PIL import Image
from ShowapiRequest import ShowapiRequest
from base.find_element import FindElement


class RegisterFunction:

    def __init__(self,url):
        self.driver = self.get_driver(url)

    def get_driver(self,url):
        driver = webdriver.Chrome()
        driver.get(url)
        driver.maximize_window()
        return driver

    def send_user_info(self,key,data):
        self.get_user_element(key).send_keys(data)



    def get_user_element(self,key):
        find_element = FindElement(self.driver)
        user_element = find_element.get_element(key)
        return user_element

    def get_random_user(self):
        '''
        自动生成用户名，5位
        :return:
        '''
        username = ''.join(random.sample('1234567890abcd', 5))
        return username

    def get_code_image(self,filename):
        '''
        获取验证码图片
        :return:
        '''
        self.driver.save_screenshot(filename)
        code_element = self.get_user_element('code_image')
        left = code_element.location['x']
        top = code_element.location['y']
        right = code_element.size['width'] + left
        height = code_element.size['height'] + top
        # 获取窗口可视范围的width和height
        html = self.driver.find_element_by_tag_name("html")

        # 设置图片重新打开的width和height
        resize_width = html.size['width']
        resize_height = html.size['height']

        # resize图片
        im = Image.open(filename)
        resize_img = im.resize((resize_width, resize_height), Image.BILINEAR)
        # 截取图片
        img = resize_img.crop((left, top, right, height))
        img.save(filename)

    def code_online(self,filename):
        '''
        解析验证码图片图片
        :return:
        '''
        self.get_code_image(filename)
        r = ShowapiRequest("http://route.showapi.com/184-4", "62626", "d61950be50dc4dbd9969f741b8e730f5")
        r.addBodyPara("typeId", "35")
        r.addBodyPara("convert_to_jpg", "0")
        r.addBodyPara("needMorePrecise", "0")
        r.addFilePara("image", filename)
        res = r.post()
        text = res.json()['showapi_res_body']['Result']
        return text


    def main(self):
        filename = 'E:/study/autostudy/image/test01.png'
        username = self.get_random_user()
        user_email = username + '@163.com'
        code_text = self.code_online(filename)
        self.send_user_info('user_email',user_email)
        self.send_user_info('user_name', username)
        self.send_user_info('password', '123456')
        self.send_user_info('code_text', code_text)
        self.get_user_element('register_button').click()
        error_code = self.get_user_element('code_text_error')
        if error_code == None:
            print('注册成功')
        else:
            self.driver.save_screenshot('E:/study/autostudy/image/errorcode.png')

        time.sleep(10)
        self.driver.close()

if __name__ == '__main__':
    register_function = RegisterFunction('http://www.5itest.cn/register')
    register_function.main()